<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册功能调试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .test-box {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            margin: 20px 0;
        }
        .status {
            font-size: 18px;
            margin: 20px 0;
            padding: 15px;
            border-radius: 5px;
        }
        .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .info { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
        }
        button:hover { background: #0056b3; }
        pre {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            text-align: left;
            overflow-x: auto;
            margin: 10px 0;
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="test-box">
        <h1>🔧 注册功能调试</h1>
        
        <div id="status" class="status info">
            填写注册信息进行测试
        </div>
        
        <form id="registerForm">
            <div>
                <label>统一社会信用代码:</label>
                <input type="text" id="creditCode" value="91110000123456789X" placeholder="18位统一社会信用代码">
            </div>
            <div>
                <label>密码:</label>
                <input type="password" id="password" value="test123" placeholder="密码">
            </div>
            <div>
                <label>确认密码:</label>
                <input type="password" id="confirmPassword" value="test123" placeholder="确认密码">
            </div>
            <button type="button" onclick="testRegister()">测试注册</button>
        </form>
        
        <div id="result"></div>
    </div>

    <div class="test-box">
        <h2>🔍 调试信息</h2>
        <button onclick="testBackendStatus()">检查后端状态</button>
        <button onclick="testCors()">测试CORS</button>
        <button onclick="testDatabase()">测试数据库</button>
        <div id="debugResult"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:8080';
        
        async function testRegister() {
            const statusDiv = document.getElementById('status');
            const resultDiv = document.getElementById('result');
            
            const creditCode = document.getElementById('creditCode').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            
            statusDiv.className = 'status info';
            statusDiv.textContent = '正在测试注册...';
            resultDiv.innerHTML = '';
            
            // 验证表单
            if (!creditCode || !password || !confirmPassword) {
                statusDiv.className = 'status error';
                statusDiv.textContent = '❌ 请填写所有字段';
                return;
            }
            
            if (password !== confirmPassword) {
                statusDiv.className = 'status error';
                statusDiv.textContent = '❌ 两次密码输入不一致';
                return;
            }
            
            try {
                const registerData = {
                    username: creditCode,
                    passwordHash: password,
                    phoneNumber: '',
                    role: 'BUYER',
                    targetCredit: 0,
                    userImage: ''
                };
                
                console.log('发送注册数据:', registerData);
                
                const response = await fetch(`${API_BASE_URL}/user/register`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(registerData)
                });
                
                console.log('响应状态:', response.status);
                console.log('响应头:', response.headers);
                
                const data = await response.json();
                console.log('响应数据:', data);
                
                if (data.code === '1') {
                    statusDiv.className = 'status success';
                    statusDiv.textContent = '✅ 注册成功！';
                    resultDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
                } else {
                    statusDiv.className = 'status error';
                    statusDiv.textContent = `❌ 注册失败: ${data.message}`;
                    resultDiv.innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
                }
            } catch (error) {
                statusDiv.className = 'status error';
                statusDiv.textContent = '❌ 注册请求失败';
                resultDiv.innerHTML = `
                    <h3>错误信息：</h3>
                    <pre>${error.message}</pre>
                    <h3>可能的原因：</h3>
                    <ul>
                        <li>后端服务未启动</li>
                        <li>CORS跨域问题</li>
                        <li>数据库连接问题</li>
                        <li>网络连接问题</li>
                    </ul>
                `;
            }
        }
        
        async function testBackendStatus() {
            const resultDiv = document.getElementById('debugResult');
            resultDiv.innerHTML = '<div class="info">正在检查后端状态...</div>';
            
            try {
                const response = await fetch(`${API_BASE_URL}/user/get-username?username=admin`);
                if (response.ok) {
                    resultDiv.innerHTML = '<div class="success">✅ 后端服务正常</div>';
                } else {
                    resultDiv.innerHTML = `<div class="error">❌ 后端响应异常: ${response.status}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">❌ 后端服务离线: ${error.message}</div>`;
            }
        }
        
        async function testCors() {
            const resultDiv = document.getElementById('debugResult');
            resultDiv.innerHTML = '<div class="info">正在测试CORS...</div>';
            
            try {
                const response = await fetch(`${API_BASE_URL}/user/register`, {
                    method: 'OPTIONS',
                    headers: {
                        'Access-Control-Request-Method': 'POST',
                        'Access-Control-Request-Headers': 'Content-Type'
                    }
                });
                
                if (response.ok) {
                    resultDiv.innerHTML = '<div class="success">✅ CORS配置正常</div>';
                } else {
                    resultDiv.innerHTML = `<div class="error">❌ CORS配置异常: ${response.status}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">❌ CORS测试失败: ${error.message}</div>`;
            }
        }
        
        async function testDatabase() {
            const resultDiv = document.getElementById('debugResult');
            resultDiv.innerHTML = '<div class="info">正在测试数据库...</div>';
            
            try {
                const response = await fetch(`${API_BASE_URL}/user/get-username?username=admin`);
                const data = await response.json();
                
                if (data) {
                    resultDiv.innerHTML = '<div class="success">✅ 数据库连接正常</div>';
                } else {
                    resultDiv.innerHTML = '<div class="error">❌ 数据库无数据</div>';
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="error">❌ 数据库连接失败: ${error.message}</div>`;
            }
        }
    </script>
</body>
</html>
